package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Lessonsituation;
import cn.sunlelin.educationserver.service.LessonsituationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class LessonsituationController {
    @Autowired
    private LessonsituationService lessonsituationService;

    @PostMapping("/lessonsituation/createLessonsituation")
    public Map<String,Object> createLessonsituation(@RequestBody Lessonsituation lessonsituation){
        Map<String,Object> map=new HashMap<>();
        if(lessonsituationService.createLessonsituation(lessonsituation)){
            map.put("code",200);
            map.put("msg","创建课时情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","创建课时情况失败，该用户对应课程已有记录");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/lessonsituation/selectLessonsituationByUserAndLesson")
    public Map<String,Object> selectLessonsituationByUserAndLesson(@RequestBody Lessonsituation lessonsituation){
        Lessonsituation lessonsituationOut=lessonsituationService.selectLessonsituationByUserAndLesson(lessonsituation);
        Map<String,Object> map=new HashMap<>();
        if(lessonsituationOut!=null){
            map.put("code",200);
            map.put("msg","根据课时与用户查询课时情况成功");
            map.put("dataobject",lessonsituationOut);
        }
        else {
            map.put("code",500);
            map.put("msg","根据课时与用户查询课时情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/lessonsituation/selectLessonsituationByLessonId/{lessonId}")
    public Map<String,Object> selectLessonsituationByLessonId(@PathVariable Integer lessonId){
        List<Lessonsituation> lessonsituationList=lessonsituationService.selectLessonsituationByLessonId(lessonId);
        Map<String,Object> map=new HashMap<>();
        if(lessonsituationList!=null){
            map.put("code",200);
            map.put("msg","根据课时Id查询课时情况成功");
            map.put("dataobject",lessonsituationList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据课时Id查询课时情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/lessonsituation/deleteLessonsituation/{lessonsituationId}")
    public Map<String,Object> deleteLessonsituation(@PathVariable Integer lessonsituationId){
        Map<String,Object> map=new HashMap<>();
        if(lessonsituationService.deleteLessonsituation(lessonsituationId)){
            map.put("code",200);
            map.put("msg","删除课时情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除课时情况失败");
            map.put("dataobject",null);
        }
        return map;
    }
}
